package com.banyuan.dao;

import com.banyuan.domain.Author;
import com.banyuan.utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AuthorDao {

    // 增加一条用户信息存放到数据库

    public static int insert(Author author) throws SQLException {
        int i = JDBCUtils.executeUpdate("insert into author(userName,password,nickName,createTime,updateTime) values(?,?,?,now(),now())",
                author.getUserName(), author.getPassword(), author.getNickName());
        return i;
    }

    // 通过传入一个用户名 来查询用户的所有记录

    public static ResultSet selectAuthorByUserName(String userName) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select count(*) as countName from author where userName=?", userName);
        return resultSet;
         // 传一个统计count 如果，这个count >0的话， 就说明重复，如果没有则没有重复
    }

    // 传入用户名和密码，去数据库里查询这个用户的所有信息
    public static ResultSet selectAuthorByUserNameAndPassword(String userName, String password) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from author where userName=? and password=?",userName,password);
        return resultSet;
    }

    // 通过id去查出这个用户的所有信息

    public static ResultSet selectAuthorById(Integer id) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from author where id = ?", id);
        return resultSet;
    }


    // 通过传入的id和密码来修改用户的密码

    public static int updatePasswordById(Integer id, String password) throws SQLException {
        int i = JDBCUtils.executeUpdate("update author set password=? where id = ?", password, id);
        return i;
    }

}
